package com.eric.server.test;

import com.eric.server.logic.DefaultLogic;
import com.eric.server.logic.ILogic;
import com.eric.server.logic.packet.ChatToGroupPacket;
import com.eric.server.logic.packet.UserRegisterPacket;
import com.eric.server.packet.Packet;
import com.sun.org.apache.bcel.internal.generic.ILOAD;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Handler implementation for the echo server.
 */
@Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {

//    ILogic logic = DefaultLogic.me();

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        ByteBuf buf = (ByteBuf) msg;
//        byte[]bytes = new byte[buf.readableBytes()];
//        buf.readBytes(bytes);
//        System.out.print("from server : "+new String(bytes,"UTF-8"));

        System.out.println(ctx);
        ctx.write(msg);


//        Packet packet = (Packet)msg;
//        switch (packet.getPacketType()){
//            case UserRegister:
//                UserRegisterPacket p = (UserRegisterPacket)packet;
//                logic.join(ctx.channel(),p.user,p.room);
//                break;
//            case ChatToGroup:
//                ChatToGroupPacket chatToGroupPacket = (ChatToGroupPacket)packet;
//                logic.sendMsg(ctx.channel(),chatToGroupPacket.msg);
//                break;
//        }

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception{
        super.channelReadComplete(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        ctx.close();
    }
}
